/*
 * @Author: 晟松
 * @Date: 2021-11-30 13:09:30
 * @LastEditors: 晟松
 * @LastEditTime: 2021-12-08 14:56:15
 * @FilePath: \mini-vue3d:\Code\node\koa2\volunteer-service\src\main\util\ValidateUtil.js
 * @Description: model column定义时的验证方法
 */
import enums from "@main/config/enums";
import region from "@main/config/region";

// const PHONE_PATTERN = /(13\d|14[579]|15[^4\D]|17[^49\D]|18\d)\d{8}/g;
// 上面的正则的 /g 全局模式有点问题
const PHONE_PATTERN = /^0?1[3|4|5|6|7|8][0-9]\d{8}$/;
/**
 * 电话号码验证
 * @param {string} val
 */
export const isPhone = (val) => {
    return PHONE_PATTERN.test(val);
};

/**
 * 空值验证
 * @param {*} val
 */
export const isNull = (val) => {
    return val === undefined || val === null;
};

export const isBlank = (val) => {
    return val === "" || isNull(val);
};

/**
 * @description: 传入的val是否在enums中定义
 * @param {*} val为数字，type为enums中导出的对象的字符串，如'gender'，'status'
 * @return {*}
 */
export const isValidateEnum = (val, type) => {
    return enums[type][val];
};

/**
 * @description: 判断传入的省是否正确，至于下面的市和区这个就不判断了，交给前端吧
 * @param {*}val 传入的省的值
 * @return {*}
 */
export const isProvince = (val) => {
    let judge = false;

    for (let i = 0; i < region.length; i++) {
      // slice(0, -1)是取str除最后一个字的全部字符
        if (
            province.provinceName == val ||
            province.provinceName.slice(0, -1) == val
        ) {
            judge = true;
            break;
        }
    }
    return judge;
};
